package huadi.concurrent.atomicclear;

import java.util.concurrent.atomic.AtomicLong;

/**
 * Created with IntelliJ IDEA.
 *
 * @author zhufeng
 *         Date: 12-7-17
 *         Time: 下午6:44
 *         To change this template use File | Settings | File Templates.
 */
public class AtomicClear {
    private AtomicLong counter = new AtomicLong(0);

    // TODO wrong
    public void incrementAndClear() {
        if (counter.get() < 50) {
            counter.getAndIncrement();
        } else {
            counter.compareAndSet(50, 0);
        }
    }

    public long get() {
        return counter.get();
    }

    public static void main(String[] args) {
        final AtomicClear ac = new AtomicClear();
        Thread t1 = new Runner(ac);
        Thread t2 = new Runner(ac);
        Thread t3 = new Runner(ac);

        t1.start();
        t2.start();
        t3.start();
    }
}
